<!--
 * @Descripttion:大区城市门店
 * @Author: caijj
 * @Date: 2020-12-03 15:14:54
 * @LastEditors: caijj
 * @LastEditTime: 2021-03-02 14:11:05
-->
<template>
  <BasicLayout>
    <template #wrapper>
      <search-form
        :report-type="formData.reportType"
        :exporting="exporting"
        @export="val => onExportReport('performance', val)"
        @submit="onSearchReport"
      />
      <tree-table
        :loading="loading"
        :columns="columns"
        :table-data="tableData"
        :load="loadChildNodes"
        :tree-node-prop="'hasChildNodes'"
        border
      />
    </template>
  </BasicLayout>
</template>
<script>
import SearchForm from '@/components/business/SearchForm';
import TreeTable from '@/components/common/TreeTable';
import { getPerformanceReport } from '@/api/commonApi';
import exportReportMixin from '@/components/mixins/ExportReport';
import Columns from '@/utils/columns-config/common';

export default {
  name: 'CommodityRegionalCity',
  components: {
    SearchForm,
    TreeTable
  },
  mixins: [exportReportMixin],
  data() {
    return {
      columns: Columns,
      tableData: [],
      loading: false,
      formData: {
        queryMode: 'REGION',
        reportType: 'REGION_CITY_SHOP'
      }
    };
  },
  methods: {
    onSearchReport(form) {
      this.loading = true;
      this.tableData = [];
      this.$nextTick(() => {
        this.formData = {
          ...this.formData,
          ...form,
          queryMode: 'REGION'
        };
        getPerformanceReport(this.formData).then(({ data, resultCode }) => {
          if (resultCode === '0000') {
            data.forEach(item => {
              item.uuid = item.businessId + item.queryMode;
            });
            this.tableData = data;
            this.loading = false;
          }
        }).catch(() => {
          this.loading = false;
        });
      });
    },
    loadChildNodes(tree, treeNode, resolve) {
      const { queryMode, businessId, childQueryMode } = tree;
      const params = {
        ...this.formData,
        queryMode: childQueryMode
      };
      if (queryMode === 'REGION') {
        params.regions = [businessId];
        this.regions = params.regions;
      } else if (queryMode === 'CITY') {
        params.regions = this.regions;
        params.cities = [parseInt(businessId)];
      }
      getPerformanceReport(params).then(({ data, resultCode }) => {
        if (resultCode === '0000') {
          data.forEach(item => {
            item.uuid = item.businessId + item.queryMode + new Date().getTime();
          });
          resolve(data);
        }
      });
    }
  }
};
</script>
